#include "UserInput.h"
#include "Common.h"
#include "../infLib/infLib.h"
#include "../sys/Sys_public.h"
#include "../renderer/RenderSystem.h"
#include "../renderer/RenderWorld.h"



//#define SHOW_DEBUG

static const float scaleUnit = 0.5f;

static int numEvents, i;

void UserInput::Keyboard() {
    int ch;
    bool state;

    i=0;
    numEvents = Sys_PollKeyboardInputEvents();
    while( numEvents-- ) {
        i = Sys_ReturnKeyboardInputEvent(i, ch, state);

        if( ch == 'P' && state == true ) {
            static int shotCounter = 0;

            inf::Str    screenShotFileName("screenshot");
            screenShotFileName += shotCounter;
            screenShotFileName += ".tga";
            renderSystem->TakeScreenshot( screenShotFileName.c_str() );

            shotCounter++;


        }
#ifdef SHOW_DEBUG
        inf::Str s( (char)ch );
        common->Printf( "Keyboard: %s, state: %d\n", s.c_str(), state );
#endif
    }
    Sys_EndKeyboardInputEvents();

}

void UserInput::Mouse() {
    int action;
    int value, value2;

    i=0;
    numEvents = Sys_PollMouseInputEvents();
    while( numEvents-- ) {
        i = Sys_ReturnMouseInputEvent(i, action, value, value2);

        if( action == M_DELTAZ ) {
            renderView_t view = renderSystem->GetPrimaryWorld()->GetMainView();

            int down = value2;
            if( down ) {
                view.viewOrg.z += scaleUnit * value;
            }else
                view.viewOrg.z -= scaleUnit * value;

            renderSystem->GetPrimaryWorld()->SetRenderView( view );
        }
#ifdef SHOW_DEBUG
        static const char* mouseLabel[] = {
            "M_ACTION1",
            "M_ACTION2",
            "M_ACTION3",
            "M_ACTION4",
            "M_ACTION5",
            "M_ACTION6",
            "M_ACTION7",
            "M_ACTION8",
            "M_DELTAX",
            "M_DELTAY",
            "M_DELTAZ"
        };

        common->Printf( "Mouse: %s, offset: %d, state: %d\n", mouseLabel[action], value, value2 );
#endif
    }

    Sys_EndMouseInputEvents();
}

void UserInput::CharKeys() {
    sysEvent_t event;

    do {
        event = Sys_GetEvent();
        if( event.evType == SE_CHAR ) {


            renderView_t view = renderSystem->GetPrimaryWorld()->GetMainView();
            if( event.evKey == 'a' ) {
                view.viewOrg.x -= scaleUnit;
            }else if( event.evKey == 'd' ) {
                view.viewOrg.x += scaleUnit;
            }
            if( event.evKey == 'w' ) {
                view.viewOrg.y -= scaleUnit;
            }else if( event.evKey == 's' ) {
                view.viewOrg.y += scaleUnit;
            }

            renderSystem->GetPrimaryWorld()->SetRenderView( view );

#ifdef SHOW_DEBUG
            inf::Str s( (char)event.evKey );
            common->Printf( "KeyChar: %s, param: %d, %d\n", s.c_str(), event.evValue, event.evValue2 );
#endif
        }
    } while( event.evType != SE_NONE );
}

void UserInput::Finish() {
    Sys_ClearEvents();
}
